<?php

class Model_Video {
    static $TABLE_GIRL_VIDEO = 'girl_video';
    
    public function get_girl_video_by_page($page, $count, $type = 1, $admin_remove = false){
         $db = Model_Connections::getInstance()->getDbConnect();
        $sql = "Select * from " . self::$TABLE_GIRL_VIDEO;
        $start_from = ($page - 1) * $count;
        $end = $count;
        if($admin_remove)
            $sql .= " WHERE video_type = ".$type;
        $sql .= ' order by id DESC ';        
        $sql .= ' LIMIT ' . $start_from . ',' . $end;
        $data = $db->fetchAll($sql);
        if($admin_remove && !empty($data)){
            foreach ($data as $key => $v) {
                unset($data[$key]['admin']);
                unset($data[$key]['video_type']);
                unset($data[$key]['view']);
                unset($data[$key]['comment']);
                unset($data[$key]['create_time']);
            }
        }
        return $data;
    }
    
    public function insert_girl_video($data) {
        if (!empty($data)) {
            $db = Model_Connections::getInstance()->getDbConnect();
            $id = $db->insert(self::$TABLE_GIRL_VIDEO, $data);
            $lastInsertId = -1;
            if ($id > 0)
                $lastInsertId = $db->lastInsertId();
            return $lastInsertId;
        }
    }

    public function delete_girl_video($id) {
        $db = Model_Connections::getInstance()->getDbConnect();
        $sql = "Delete from " . self::$TABLE_GIRL_VIDEO;
        $sql .= ' where id = ' . $id;
        return $db->query($sql);
    }

    public function update_girl_video($id, $data) {
        $db = Model_Connections::getInstance()->getDbConnect();
        $sql = "Update " . self::$TABLE_GIRL_VIDEO;
        $sql .= " SET ";
        $sql .= "`video_type`" . "='" . $data['video_type'] . "',";
        $sql .= "`title`" . "='" . $data['title'] . "',";
        $sql .= "`thumb`" . "='" . $data['thumb'] . "',";
        $sql .= "`link`" . "='" . $data['link'] . "',";
        $sql .= "`admin`" . "='" . $data['admin'] . "'";
        $sql .= ' where id = ' . $id;

        return $db->query($sql);
    }
    
    public function get_girl_video_total() {
        $db = Model_Connections::getInstance()->getDbConnect();
        $count = $db->fetchOne('SELECT COUNT(*) AS count FROM ' . self::$TABLE_GIRL_VIDEO);
        return $count;
    }
    public function get_girl_video_for_update_by_id($id) {
        $db = Model_Connections::getInstance()->getDbConnect();
        $sql = "Select * from " . self::$TABLE_GIRL_VIDEO;
        $sql .= ' where `id` = ' . $id;
        return $db->fetchAll($sql);
    }
}

?>
